#import <AssetsLibrary/AssetsLibrary.h>
#import "CreativeWallpaperImagePreviewViewController.h"
#import "CreativeWallpaperImageGenerator.h"
#import "CreativeWallpaperCreativeWallpaperAlertViewController.h"
#import "SVProgressHUD.h"
@interface CreativeWallpaperImagePreviewViewController ()
@property IBOutlet UILabel *clockLabel;
@property IBOutlet UILabel *dateLabel;
@property IBOutlet UIButton *changeBackgroundButton;
@property IBOutlet UIButton *cancelButton;
@property IBOutlet UIButton *saveButton;
- (void)imageDidFinishSavingWithError:(NSError *)error;
- (IBAction)changeImageButtonPressed:(id)sender;
- (IBAction)cancelButtonPressed:(id)sender;
- (IBAction)saveButtonPressed:(id)sender;
@end
static NSString * const kSavedPhotosAlbumName = @"CreativePersonalityWallpaper Wallpapers";
@implementation CreativeWallpaperImagePreviewViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.changeBackgroundButton.titleLabel.font = [UIFont applicationFontOfSize:16.0f];
    self.cancelButton.titleLabel.font = [UIFont applicationFontOfSize:18.0f];
    self.saveButton.titleLabel.font = [UIFont applicationFontOfSize:18.0f];
    NSArray *clockFontAttributes = @[@{ UIFontFeatureTypeIdentifierKey: @(6),
                                        UIFontFeatureSelectorIdentifierKey: @(1) },
                                     @{ UIFontFeatureTypeIdentifierKey: @(17),
                                        UIFontFeatureSelectorIdentifierKey: @(1) }];
    UIFont *clockFont = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:90.0f];
    UIFontDescriptor *clockFontDescriptor = [[clockFont fontDescriptor] fontDescriptorByAddingAttributes: @{ UIFontDescriptorFeatureSettingsAttribute: clockFontAttributes }];
    self.clockLabel.font = [UIFont fontWithDescriptor:clockFontDescriptor size:0.0f];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"h:mm"];
    self.clockLabel.text = [formatter stringFromDate:[NSDate date]];
    [formatter setDateFormat:@"EEEE, MMMM d"];
    self.dateLabel.text = [formatter stringFromDate:[NSDate date]];
}
- (void)imageDidFinishSavingWithError:(NSError *)error {
    if (error) {
        [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"Error saving wallpaper", nil)];
    } else {
        [SVProgressHUD dismiss];
        UIViewController *presentingViewController = self.presentingViewController;
        [presentingViewController dismissViewControllerAnimated:YES completion:nil];
        CreativeWallpaperCreativeWallpaperAlertViewController *alertViewController = [[CreativeWallpaperCreativeWallpaperAlertViewController alloc] initWithNibName:nil bundle:nil];
        alertViewController.title = NSLocalizedString(@"Wallpaper Saved", nil);
        alertViewController.message = NSLocalizedString(@"Your wallpaper was saved to the 'CreativePersonalityWallpaper Wallpapers' album in the Photos app", nil);
        [alertViewController addAction:[CreativeWallpaperCreativeWallpaperAlertAction actionWithTitle:NSLocalizedString(@"Ok", nil) style:UIAlertActionStyleCancel handler:^(CreativeWallpaperCreativeWallpaperAlertAction *action) {
            [presentingViewController dismissViewControllerAnimated:YES completion:nil];
        }]];
        [presentingViewController presentViewController:alertViewController animated:YES completion:nil];
    }
}
- (void)setReminders:(NSArray *)reminders {
    _reminders = reminders;
    self.imageView.image = [CreativeWallpaperImageGenerator wallpaperImageWithBackground:self.backgroundImage reminders:self.reminders];
}
- (void)setBackgroundImage:(UIImage *)backgroundImage {
    _backgroundImage = backgroundImage;
    self.imageView.image = [CreativeWallpaperImageGenerator wallpaperImageWithBackground:self.backgroundImage reminders:self.reminders];
}
#pragma mark - Actions
- (IBAction)changeImageButtonPressed:(id)sender {
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
- (IBAction)cancelButtonPressed:(id)sender {
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)saveButtonPressed:(id)sender {
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    [assetsLibrary writeImageToSavedPhotosAlbum:self.imageView.image.CGImage
                                    orientation:(ALAssetOrientation)self.imageView.image.imageOrientation
                                completionBlock:^(NSURL *assetURL, NSError *error) {
                                    [assetsLibrary assetForURL:assetURL
                                                   resultBlock:^(ALAsset *asset) {
                                                       __block BOOL albumCreated = NO;
                                                       [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum
                                                                                    usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                                                                                        if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:kSavedPhotosAlbumName]) {
                                                                                            [group addAsset:asset];
                                                                                            albumCreated = YES;
                                                                                            *stop = YES;
                                                                                            [self imageDidFinishSavingWithError:error];
                                                                                        }
                                                                                        if (!group && !albumCreated) {
                                                                                            [assetsLibrary addAssetsGroupAlbumWithName:kSavedPhotosAlbumName
                                                                                                                           resultBlock:^(ALAssetsGroup *group) {
                                                                                                                               [group addAsset:asset];
                                                                                                                               [self imageDidFinishSavingWithError:nil];
                                                                                                                           } failureBlock:^(NSError *error) {
                                                                                                                               [self imageDidFinishSavingWithError:error];
                                                                                                                           }];
                                                                                        }
                                                                                    } failureBlock:^(NSError *error) {
                                                                                        [self imageDidFinishSavingWithError:error];
                                                                                    }];
                                                   } failureBlock:^(NSError *error) {
                                                       [self imageDidFinishSavingWithError:error];
                                                   }];
                                }];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    self.backgroundImage = info[UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)isSimpleUniversalTravelRecordsYl21BHmf8Gm1bMrtbMQM:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleUniversalTravelRecordsNRAg6O1ecvhk8MphrtB:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleUniversalTravelRecordsL3K42yhrruSf5:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleUniversalTravelRecordstnTdYAMgU8szKs8eu9E9quH0FB:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleUniversalTravelRecords2Lqo2NPqQFgHY:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleUniversalTravelRecordsW7sgQUhR1w6zcQj8a7e:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleUniversalTravelRecordsMYTGb9Naf8q2AnAwahoi:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}

- (void)isSimpleUniversalTravelRecordsqhm9DPB5cGptvbIdkDDIVr6gc:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleUniversalTravelRecordsGsd2veMvAHVd1W66wP:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleUniversalTravelRecordsx8tXRWg966Vxv12:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleUniversalTravelRecordsaJwzFk15IU45eHvfa72n0:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleCreativeWallpaperFslz7hMsbOscQ7znixI5il:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleCreativeWallpaperKoxeKN8dxzml:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaperIbnsWCF9TIL0EiQyzH4xhygR:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleCreativeWallpaperkPsYp6e9gSwct8KyrOdJ2Mt:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleCreativeWallpaperdphU8fzpcVqo0TS1oz:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleCreativeWallpaperBmQqXlDn7VygwcgT6iA80up19O:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleCreativeWallpapervz36Bt6SxstlF:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleCreativeWallpaperWBbCXtBIhv9o4WHexnTXfGpf:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleCreativeWallpaperqBbsod3N55Kq3HXpUNnlz:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleCreativeWallpaperGQcu2jGjhFez:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaperjBUWqWcmf0IPf3XCL79nox0FK:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleCreativeWallpaperbvuzT08qVV0QPQRcjVE:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleCreativeWallpaperlb1ItLvkxhKS:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleCreativeWallpaperq4RyBoRJXBrd:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleCreativeWallpaperCAx9a4wVe5UhFjlW:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleCreativeWallpaperYqvPqEyQp95Si3lX6kEzbV:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleCreativeWallpapereO5GuDhmSf9Npa:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaperkb5r15IbTj240UxMTW:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaper7pM0XRyh8uOltQ2iR2Nv9gRs5O:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleCreativeWallpaperKtUGJNK4oUaH2jSCF4MGaqKzil:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaperhzLctELd95uKW1AeMvL:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleCreativeWallpaper94YLreTmC53is9O35ve:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}
@end
